﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.manager
{
    public partial class sysroleedit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Request["roleid"]))
            {
                throw new Base.CustomException("缺少参数roleid");
            }

            Hashtable role = PhoneBook.Manage.SysRole.get(Request["roleid"]);
            if (role == null)
            {
                throw new Base.CustomException("参数roleid不正确");
            }

            if (!string.IsNullOrEmpty(Commons.StrUtil.FormatValue(role["groupid"])))
            {
                throw new Base.CustomException("无权限修改此角色");
            }

            if (!IsPostBack)
            {
                txb_rolename.Text = Commons.StrUtil.FormatValue(role["rolename"]);
                txb_description.Text = Commons.StrUtil.FormatValue(role["description"]);
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            Hashtable sysrole = PhoneBook.Manage.SysRole.get(Request["roleid"]);
            addLog(sysrole);
            sysrole["rolename"] = txb_rolename.Text;
            sysrole["description"] = txb_description.Text;

            PhoneBook.Manage.SysRole.edit(sysrole);
            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
        }

        //添加日志
        private void addLog(Hashtable sysrole)
        {
            string content = string.Empty;

            if (sysrole["rolename"].ToString() != txb_rolename.Text)
                content += string.Format("角色名称:{0}->{1};", sysrole["rolename"], txb_rolename.Text);

            if (sysrole["description"].ToString() != txb_description.Text)
                content += string.Format("描述:{0}->{1};", sysrole["description"], txb_description.Text);

            if (!string.IsNullOrEmpty(content))
            {
                content = string.Format("修改了角色信息({0})。{1}", sysrole["rolename"], content);

                AddLog(content);
            }
        }
    }
}